package utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

//一个发送邮件的工具方法
public class SendMail {
    public static void sendMail(String email,String password) throws MessagingException {
        Properties props = new Properties();//相当于生成一个xxx.properties文件
        //设置主机
        props.setProperty("mail.host", "smtp.qq.com");
        //设置传输协议
        props.setProperty("mail.transport.protocol", "smtp");
        //设置允许邮箱授权认证
        props.setProperty("mail.smtp.auth", "true");
        //邮箱授权的认证
        //创建认证器对象
        Auth auth=new Auth();
        //获取一个session
        Session session=Session.getDefaultInstance(props,auth);
        //获取连接
        Transport transport = session.getTransport();
        //连接服务器
        transport.connect("smtp.qq.com", "2493554404@qq.com", "yhjuvuoqpeelecea");

        //创建邮件对象
        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress("2493554404@qq.com"));
        //设置发件人地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //邮件标题
        message.setSubject("查看重置密码");
        String content="您重置后的密码为: "+password+"\n 凭此密码可登录购物系统，注意查收";
        //邮件内容
        message.setContent(content,"text/html;charset=utf-8");
        //发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        //关闭连接
        transport.close();
        System.out.println("密码重置成功~");

    }
}
